const express = require('express');
const pool = require('../db/mysql');
const uid = require('../db/uid');

const router = express.Router();

router.get( '/topic/list' , (req,resp)=>{

    let sql = `SELECT t.id id , t.title , t.publish_time time , c.nickname author , c.id cid 
               FROM t_topics t JOIN t_customers c ON t.customer_id = c.id 
               ORDER BY time DESC`;

    let handler = (error,results,fields)=>{
        if( error ) {
            throw error ;
        }
        let customer = req.session.customer ;
        resp.render( 'topic-list' , { c: customer , topics: results } );
    }
    pool.execute( sql , handler );
});

router.get( '/topic/publish' , (req,resp)=>{
    let customer = req.session.customer ;
    if( customer ) {
        resp.render( 'topic-publish' , { c: customer } );
        return ;
    }
    resp.redirect( '/customer/sign/in' );
});

router.post( '/topic/publish' , (req,resp)=>{
    let customer = req.session.customer ;
    if( customer ) {
        let id = uid.idGenerator();
        let title = req.body.title ;
        let content = req.body.content ;
        let cid = customer.id ;
        let time = new Date();
        // let ip = req.ip ;
        let ip = req.ip.match(/\d+\.\d+\.\d+\.\d+/);
        ip = ip == null ? ip : ip[0] ;

        let sql = `INSERT INTO t_topics 
                   ( id , title , content , customer_id , publish_time , publish_ip )
                   VALUES 
                   ( ? , ? , ? , ? , ? , ? )` ;

        let params = [ id , title , content , cid , time , ip ];
        let handler = (error,results,fields) =>{
            if( error ) {
                throw error ;
            }
            resp.redirect( '/topic/list' );
        }
        pool.execute( sql , params , handler );
        return ;
    }
    resp.redirect( '/customer/sign/in' );
});

router.get( '/topic/detail/:id' , (req,resp) => {
    let topicId = req.params.id ;
    let sql = `SELECT t.id id , t.title , t.content , t.publish_time time , c.nickname author
               FROM t_topics t JOIN t_customers c ON t.customer_id = c.id 
               WHERE t.id = ? `;
    let params = [ topicId ];
    let handler = (error,results,fields)=>{
        if( error ) {
            throw error ;
        }

        let topic = results[ 0 ]; // 从查询结果数组中获得 topic

        let q = `SELECT m.id , m.content , m.comment_time time , c.nickname author 
                 FROM t_comments m JOIN t_customers c ON m.customer_id = c.id 
                 WHERE m.topic_id = ? 
                 ORDER BY time DESC`;
        pool.execute( q , [ topic.id ] , (error,rows)=>{
            let customer = req.session.customer ;
            resp.render( 'topic-detail' , { c: customer , t: topic , comments: rows } );
        });
    }
    pool.execute( sql , params , handler );
} );

router.get( '/topic/mine' ,  (req,resp) => {
    let customer = req.session.customer ;
    if( customer ) {
        let sql = `SELECT t.id id , t.title , t.publish_time time 
                   FROM t_topics t 
                   WHERE t.customer_id = ?  
                   ORDER BY time DESC`;
        let params = [ customer.id ];
        let handler = (error,results,fields)=>{
            if( error ) {
                throw error ;
            }
            resp.render( 'topic-mine' , { c: customer , topics: results } );
        }
        pool.execute( sql , params , handler );
        return ;
    }
    resp.redirect( '/customer/sign/in' );
});

router.get( '/topic/edit/:id' ,  (req,resp) => {
    let topicId = req.params.id ;
    let sql = `SELECT t.id id , t.title , t.content , t.publish_time time , c.nickname author
               FROM t_topics t JOIN t_customers c ON t.customer_id = c.id 
               WHERE t.id = ? `;
    let params = [ topicId ];
    let handler = (error,results,fields)=>{
        if( error ) {
            throw error ;
        }
        let topic = results[ 0 ];
        let customer = req.session.customer ;
        resp.render( 'topic-edit' , { c: customer , t: topic } );
    }
    pool.execute( sql , params , handler );
});

module.exports = router ;